// VBConversions Note: VB project level imports
using System.Collections.Generic;
using System;
using System.Linq;
using System.Drawing;
using System.Diagnostics;
using Microsoft.VisualBasic;
using System.Xml.Linq;
using Microsoft.VisualBasic.Compatibility;
using System.Windows.Forms;
using System.Collections;
using System.Data;
// End of VB project level imports

using System.Threading;
using System.IO;


namespace 喷码数据生成工具
{
	public partial class frm_Timeidan
	{
		public frm_Timeidan()
		{
			InitializeComponent();
		}
		public struct DataStructure
		{
			public string 本号;
			public string 票号;
			public string 条码数据;
		}
		List<DataStructure> Data = new List<DataStructure>();
		Thread th;
		const int BHCount = 25;
		
		public void frm_Timeidan_Load(object sender, System.EventArgs e)
		{
			CommFunction.ChangeButtonFromFrm(this.GroupBox1);
			CommFunction.ChangeButtonFromFrm(this.GroupBox2);
			CommFunction.ChangeButtonFromFrm(this.GroupBox3);
			TB_BHLength.KeyPress += HandleTextKeyPress;
			TB_PHLength.KeyPress += HandleTextKeyPress;
			TB_PHStartNo.KeyPress += HandleTextKeyPress;
			TB_PHEndNo.KeyPress += HandleTextKeyPress;
			TB_BHStartNo.KeyPress += HandleTextKeyPress;
		}
		private void HandleTextKeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (Strings.Asc(e.KeyChar) == 8)
			{
				return ;
			}
			if (Strings.Asc(e.KeyChar) < 47 || Strings.Asc(e.KeyChar) > 57)
			{
				e.Handled = true;
			}
		}
		public void TB_DataFileName_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			e.Handled = true;
		}
		
		public void Btn_OpenDialog_Click(System.Object sender, System.EventArgs e)
		{
			OpenFileDialog dialog = new OpenFileDialog();
			dialog.Title = "选择客供号码数据";
			dialog.CheckFileExists = true;
			dialog.Filter = "文本文件(*.txt)|*.txt";
			if (!string.IsNullOrEmpty(TB_SavePath.Text))
			{
				dialog.InitialDirectory = TB_SavePath.Text;
			}
			if (dialog.ShowDialog() == Windows.Forms.DialogResult.OK)
			{
				this.TB_DataFileName.Text = dialog.FileName;
				this.TB_DataFileName.SelectionStart = dialog.FileName.Length;
				if (string.IsNullOrEmpty(TB_SavePath.Text))
				{
					TB_SavePath.Text = CommFunction.getOnlyPath(TB_DataFileName.Text);
				}
				TB_Name.Text = CommFunction.getOnlyName(dialog.FileName);
			}
		}
		
		public void Btn_OpenFolder_Click(System.Object sender, System.EventArgs e)
		{
			FolderBrowserDialog dialog = new FolderBrowserDialog();
			dialog.Description = "选择生成数据保存路径";
			dialog.RootFolder = Environment.SpecialFolder.Desktop;
			dialog.ShowNewFolderButton = true;
			if (dialog.ShowDialog() == Windows.Forms.DialogResult.OK)
			{
				TB_SavePath.Text = dialog.SelectedPath;
				TB_SavePath.SelectionStart = dialog.SelectedPath.Length;
			}
		}
		
		public void Btn_ReadData_Click(System.Object sender, System.EventArgs e)
		{
			if (File.Exists(TB_DataFileName.Text))
			{
				SetObjectEnabled(false, GroupBox2);
				SetObjectEnabled(false, GroupBox3);
				FileStream fs = new FileStream(TB_DataFileName.Text, FileMode.Open, FileAccess.Read);
				StreamReader fw = new StreamReader(fs, System.Text.Encoding.Default);
				string ErrText = string.Empty;
				while (!fw.EndOfStream)
				{
					string tmp = fw.ReadLine();
					if (!string.IsNullOrEmpty(tmp))
					{
						int a = tmp.IndexOf(",");
						if (a > 0)
						{
							string[] str = tmp.Split(',');
							if (str.Length != 3)
							{
								MessageBox.Show(this, "数据格式错误，请确认数据格式是否正确！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
							}
							fw.Close();
							fs.Close();
							Data.Clear();
							goto EndLine;
							DataStructure tmpItem = new DataStructure();
							tmpItem.票号 = str[0];
							tmpItem.条码数据 = str[1];
							tmpItem.本号 = str[2];
							if (Data == null)
							{
								Data = new List<DataStructure>();
							}
							Data.Add(tmpItem);
							(new Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase()).DoEvents();
						}
						else if (!(string.IsNullOrEmpty(tmp)))
						{
							MessageBox.Show(this, "数据格式错误，请确认数据格式是否正确！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
							fw.Close();
							fs.Close();
							Data.Clear();
							goto EndLine;
						}
					}
				}
				fw.Close();
				fs.Close();
				MessageBox.Show(this, "数据读取结束，共有：" + System.Convert.ToString(Data.Count) + "条数据！", "结束", MessageBoxButtons.OK, MessageBoxIcon.Information);
				ShowSampleData(RadioButton1.Checked);
EndLine:
				SetObjectEnabled(true, GroupBox2);
				SetObjectEnabled(true, GroupBox3);
			}
			else
			{
				MessageBox.Show(this, "文件不存在，请重新选择数据文件！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		private void SetObjectEnabled(bool Enabled, Control ControlPanel)
		{
			foreach (Control item in ControlPanel.Controls)
			{
				item.Enabled = Enabled;
			}
		}
		private void ShowSampleData(bool TiaoMa)
		{
			if (Data.Count <= 0)
			{
				return ;
			}
			if (TiaoMa)
			{
				int SPS = (int) ((double) Data.Count / 2);
				ListBox1.Items.Clear();
				ListBox1.Items.Add("Var1   " + Data[0].本号);
				ListBox1.Items.Add("Var2   " + Data[0].票号);
				ListBox1.Items.Add("Var3   " + Data[0].条码数据);
				ListBox1.Items.Add("Var4   " + Data[SPS].本号);
				ListBox1.Items.Add("Var5   " + Data[SPS].票号);
				ListBox1.Items.Add("Var6   " + Data[SPS].条码数据);
			}
			else
			{
				int SPS = (int) ((double) Data.Count / 2);
				ListBox1.Items.Clear();
				ListBox1.Items.Add("Var1   " + Data[0].本号);
				ListBox1.Items.Add("Var2   " + Data[0].票号);
				ListBox1.Items.Add("Var3   ");
				ListBox1.Items.Add("Var4   " + Data[SPS].本号);
				ListBox1.Items.Add("Var5   " + Data[SPS].票号);
				ListBox1.Items.Add("Var6   ");
			}
		}
		
		public void Btn_CreateFM_Click(System.Object sender, System.EventArgs e)
		{
			long StartP = 0;
			long EndP = 0;
			StartP = (long) (Conversion.Val(TB_PHStartNo.Text));
			EndP = (long) (Conversion.Val(TB_PHEndNo.Text));
			if (EndP <= StartP)
			{
				MessageBox.Show(this, "起始票号不能大于或等于终止票号！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			return ;
//			if (Directory.Exists(TB_SavePath.Text) == false)
//			{
//				MessageBox.Show(this, "保存路径不存在，请先选择数据保存路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//				}
//				return ;
//				if (string.IsNullOrEmpty(TB_Name.Text))
//				{
//					MessageBox.Show(this, "保存文件名不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//					}
//					return ;
//					if (System.Convert.ToInt32(System.Convert.ToInt32(EndP - StartP) + 1) % 2 == 0)
//					{
//						long PL = (long) (Conversion.Val(TB_PHLength.Text));
//						long BL = (long) (Conversion.Val(TB_BHLength.Text));
//						string PPrefix = TB_PHPrefix.Text;
//						string BPrefix = TB_BHPrefix.Text;
//						long OneEnd = System.Convert.ToInt64(System.Convert.ToInt32(System.Convert.ToInt32(EndP - StartP) + 1) / 2 + StartP - 1);
//						long HalfCount = System.Convert.ToInt64(System.Convert.ToInt32(System.Convert.ToInt32(EndP - StartP) + 1) / 2);
//						long HalfBHCount = System.Convert.ToInt64(((System.Convert.ToInt32(EndP - StartP) + 1) / 25) / 2);
//						long BHIndex = 0;
//						long StartBH = (long) (Conversion.Val(TB_BHStartNo.Text));
//						string FileName = TB_SavePath.Text;
//						NeroBar1.Maximum = System.Convert.ToDouble((System.Convert.ToInt32(System.Convert.ToInt32(EndP - StartP) + 1) / 2) / BHCount);
//						if (FileName.Substring(FileName.Length - 1, 1) != "\\")
//						{
//							FileName = FileName + "\\";
//							}
//							FileStream fs = new FileStream(FileName + TB_Name.Text + "封面.txt", FileMode.Create, FileAccess.Write);
//							StreamWriter fw = new StreamWriter(fs, System.Text.Encoding.Default);
//							for (var i = StartP; i <= OneEnd; i += BHCount)
//							{
//								fw.WriteLine(BPrefix + System.Convert.ToString(CommFunction.FormatByLength(System.Convert.ToInt64(StartBH + BHIndex), BL)));
//								fw.WriteLine((PPrefix + CommFunction.FormatByLength(System.Convert.ToInt64(i), PL)) + "-" + (PPrefix + CommFunction.FormatByLength(System.Convert.ToInt64(System.Convert.ToInt32(i) + BHCount - 1), PL)));
								//================
//								fw.WriteLine(BPrefix + System.Convert.ToString(CommFunction.FormatByLength(System.Convert.ToInt64(System.Convert.ToInt32(StartBH + BHIndex) + HalfBHCount), BL)));
//								fw.WriteLine(PPrefix + CommFunction.FormatByLength(System.Convert.ToInt64(HalfCount + System.Convert.ToInt32(i)), PL) + "-" + (PPrefix + CommFunction.FormatByLength(HalfCount + System.Convert.ToInt32(i) + BHCount - 1, PL)));
//								NeroBar1.Value = System.Convert.ToDouble(System.Convert.ToInt32(i) - StartP + 1);
//								BHIndex++;
//								}
//								fw.Close();
//								fs.Close();
//								Interaction.MsgBox("数据生产结束，文件保存在：" + FileName + TB_Name.Text + "封面.txt", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示");
//								}
//								else
//								{
//									Interaction.MsgBox("双拼数据号码总数必须是2的倍数！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示");
//									}
								}
								
								public void TB_SavePath_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
								{
									e.Handled = true;
								}
								
								public void RadioButton1_CheckedChanged(System.Object sender, System.EventArgs e)
								{
									ShowSampleData(RadioButton1.Checked);
								}
								
								public void RadioButton2_CheckedChanged(object sender, System.EventArgs e)
								{
									ShowSampleData(RadioButton1.Checked);
								}
								
								public void GdButton4_Click(System.Object sender, System.EventArgs e)
								{
									if (Data.Count <= 0)
									{
										return ;
									}
									int ForU = (int) ((double) Data.Count / 2);
									string FileName = "";
									if (Directory.Exists(TB_SavePath.Text) == false)
									{
										MessageBox.Show(this, "保存路径不存在，请先选择数据保存路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
									}
									return ;
//									if (string.IsNullOrEmpty(TB_Name.Text))
//									{
//										MessageBox.Show(this, "保存文件名不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//										}
//										return ;
//										string tmp = TB_SavePath.Text;
//										if (tmp.Substring(tmp.Length - 1, 1) != "\\")
//										{
//											tmp = tmp + "\\";
//											}
//											if (RadioButton1.Checked)
//											{
//												FileName = tmp + TB_Name.Text + "-2.txt";
//												}
//												else
//												{
//													FileName = tmp + TB_Name.Text + "-1.txt";
//													}
//													SetObjectEnabled(false, GroupBox2);
//													RadioButton1.Enabled = false;
//													RadioButton2.Enabled = false;
//													if (File.Exists(FileName))
//													{
//														if (MessageBox.Show(this, "文件已经存在是否覆盖原文件？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == Windows.Forms.DialogResult.Cancel)
//														{
//															FileName = CommFunction.getAddFileName(FileName);
//															}
//															}
//															bool isTiaoma = RadioButton1.Checked;
//															FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write);
//															StreamWriter fw = new StreamWriter(fs, System.Text.Encoding.Default);
//															NeroBar2.Maximum = ForU;
//															for (var i = 0; i <= ForU - 1; i++)
//															{
//																fw.WriteLine(Data[System.Convert.ToInt32(i)].本号);
//																fw.WriteLine(Data[System.Convert.ToInt32(i)].票号);
//																if (isTiaoma)
//																{
//																	fw.WriteLine(Data[System.Convert.ToInt32(i)].条码数据);
//																	}
//																	else
//																	{
//																		if ((i + 1) % BHCount == 0)
//																		{
//																			fw.WriteLine("-----------------------------------------------");
//																			}
//																			else
//																			{
//																				fw.WriteLine("");
//																				}
//																				}
																				//====================================
//																				fw.WriteLine(Data[System.Convert.ToInt32(i + ForU)].本号);
//																				fw.WriteLine(Data[System.Convert.ToInt32(i + ForU)].票号);
//																				if (isTiaoma)
//																				{
//																					fw.WriteLine(Data[System.Convert.ToInt32(i + ForU)].条码数据);
//																					}
//																					else
//																					{
//																						if ((i + 1) % BHCount == 0)
//																						{
//																							fw.WriteLine("------------------------------------------------");
//																							}
//																							else
//																							{
//																								fw.WriteLine("");
//																								}
//																								}
//																								NeroBar2.Value = System.Convert.ToDouble(i + 1);
//																								(new Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase()).DoEvents();
//																								}
//																								fw.Close();
//																								fs.Close();
//																								SetObjectEnabled(true, GroupBox2);
//																								RadioButton1.Enabled = true;
//																								RadioButton2.Enabled = true;
//																								MessageBox.Show(this, "数据生成结束！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
																							}
																							
																							public void GroupBox2_Enter(object sender, EventArgs e)
																							{
																								
																							}
																						}
																					}
